using System;
using System.Data;
using System.Data.Services.Client;
using System.Diagnostics;
using System.Linq;
using Common.Unity.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NorthwindEFModel;
using Testing.Core;
using Common.Unity.Extensions;

namespace Common.Unity.Extensions.Entity.Testing
{
	[TestClass]
	[DeploymentItem( "Common.Unity.Extensions.Entity.Testing/Resources/NorthwindEF.mdf" )]
	[DeploymentItem( "Common.Unity.Extensions.Entity.Testing/Resources/NorthwindEFModel.csdl" )]
	[DeploymentItem( "Common.Unity.Extensions.Entity.Testing/Resources/NorthwindEFModel.msl" )]
	[DeploymentItem( "Common.Unity.Extensions.Entity.Testing/Resources/NorthwindEFModel.ssdl" )]
	public class EntityCreationPolicyTests : UnityContainerTestBase
	{
		[TestMethod, Owner( TestOwners.MichaelD_Bang )]
		public void VerifyExistingEntityIsRetrievedCorrectly()
		{
			var context = Container.Resolve<NorthwindEntities>();
			var key = MetadataHelper.CreateKey<Supplier, int>( context, 7 );
			var supplier = Container.Resolve<Supplier>( key );
			Assert.IsNotNull( supplier );
			Assert.AreEqual( 7, supplier.SupplierID );
			Assert.AreEqual( EntityState.Unchanged, supplier.EntityState );
		}

		[TestMethod, Owner( TestOwners.MichaelD_Bang )]
		public void VerifyNonExistingEntityIsCreatedCorrectly()
		{
			var context = Container.Resolve<NorthwindEntities>();
			var key = MetadataHelper.CreateKey<Supplier, int>( context, 7000 );
			var supplier = Container.Resolve<Supplier>( key );

			// Ensure object was added:
			Assert.AreEqual( EntityState.Added, supplier.EntityState );
			Assert.IsNotNull( supplier.EntityKey );

			// Ensure default properties set:
			Assert.AreEqual( "<New Supplier>", supplier.CompanyName );
			Assert.AreEqual( "http://codeplex.com/unitycontributions", supplier.HomePage );
			Assert.IsNotNull( supplier.FeaturedProduct );
			Assert.IsNull( supplier.BackupFeaturedProduct );
			Assert.AreEqual( new DateTime( 2008, 6, 7 ), supplier.Added );
			Assert.AreEqual( DateTime.Today, supplier.Created.Date );
		}
	}
}